package com.sinosoft.model.service.impl.Profitcal;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Hashtable;
import java.util.List;

/**
 * ?????????????
 * 
 * @author lbh
 * 
 */
public class PolSubDataHandler {
	/**
	 * ??????
	 */
	private Hashtable ht = null;

	public PolSubDataHandler(Hashtable inHt) {
		this.ht = inHt;
	}

	/**
	 * ????????????????????
	 * 
	 * @param inInvestRiskDataArrays
	 *            ????
	 * @param tableType
	 *            ??????? double[] double[][] 1,2
	 * @param tablePattern
	 *            ?????? "???????-????-??"?? 1,2,3,4
	 * @param dataType
	 *            ??????? type ???????????????
	 * @return
	 */
	public Object calculate(int tableType, int tablePattern, int dataType) {

		PolSubDataBean[] inInvestRiskDataArrays=(PolSubDataBean[])ht.get("$SubPD");//????????
		String sby3 = this.ht.get("StandbyFlag3").toString();// ???
		int period = Integer.parseInt(ht.get("Years").toString());// ???????
		int appage = Integer.parseInt(ht.get("AppAge").toString()); // ???????

		if (1 == tableType) {
			if (1 == tablePattern) {
				return getPolSubData1(dataType+"", sby3, period,
						inInvestRiskDataArrays, appage);
			} else if (2 == tablePattern) {
				return getPolSubData2(dataType+"", sby3, period,
						inInvestRiskDataArrays, appage);
			} else if (3 == tablePattern) {
				return getPolSubData3(dataType+"", sby3, period,
						inInvestRiskDataArrays, appage);
			} else if (4 == tablePattern) {
				return getPolSubData4(dataType+"", sby3, period,
						inInvestRiskDataArrays, appage);
			}
		}else if(2==tableType){
			if (1 == tablePattern) {
				return getPolSubDatas1(dataType+"", sby3, period,
						inInvestRiskDataArrays, appage);
			} else if (2 == tablePattern) {
				return getPolSubDatas2(dataType+"", sby3, period,
						inInvestRiskDataArrays, appage);
			} else if (3 == tablePattern) {
				return getPolSubDatas3(dataType+"", sby3, period,
						inInvestRiskDataArrays, appage);
			} else if (4 == tablePattern) {
				return getPolSubDatas4(dataType+"", sby3, period,
						inInvestRiskDataArrays, appage);
			}
		}

		return new double[0];
	}

	// ///////////////////////////////////////////////////////////////////////////////////////////////
	// ////??????? ???? ?? ???
	// //////////////////////////////////////////////////////////////////////
	// //////////////////////////////////////////////////////////////////////////////////////////////
	/**
	 * ???????????[???????????]
	 * 
	 * @param type
	 * @param signNo
	 * @param peroid
	 * @param inInvestRiskDataSet
	 * @return
	 */
	private double[] getPolSubData1(String type, String signNo, int peroid,
			PolSubDataBean[] inInvestRiskDataSet, int appage) {
		List<PolSubDataBean> tempInvestRiskDataSet = new ArrayList();
		int size = 0;
		if (signNo != null && !signNo.equals("")
				&& (size = inInvestRiskDataSet.length) > 0) {
			for (int i = 0; i < size; i++) {
				PolSubDataBean tempInvestRiskDataSchema = inInvestRiskDataSet[i];
				if (type.equals(tempInvestRiskDataSchema.type)
						&& tempInvestRiskDataSchema.signNo.equals(signNo)) {
					tempInvestRiskDataSet.add(tempInvestRiskDataSchema);
				}
			}
		}

		double result[] = new double[peroid + 1];
		Arrays.fill(result, 0);
		int counts = 0;
		if ((counts = tempInvestRiskDataSet.size()) > 0) {
			int index = 1;
			result[0] = 0;
			for (; index < peroid + 1; index++) {
				// int absIndex = index + 1;
				double value = 0;
				for (int j = 0; j < counts; j++) {
					PolSubDataBean tempInvestRiskDataSchema = tempInvestRiskDataSet.get(j);
					
					String sage = tempInvestRiskDataSchema.age;
					String sdegree = tempInvestRiskDataSchema.degree;
					String srs = tempInvestRiskDataSchema.value;
					
					sage = sage ==null? "0":sage;
					sdegree = sdegree ==null? "0":sdegree;
					srs = srs ==null? "0":srs;
					
					
					int age = Integer.valueOf(sage);
					int degree = Integer.valueOf(sdegree);
					double rs = Integer.valueOf(srs);
					if (age <= index + appage && age + degree > index + appage) {
						// ??��???
						value += rs;
					}
				}
				result[index] = value;
			}
		}
		return result;
	}

	/**
	 * ???????????[ ??????? ??????????]
	 * 
	 * @param type
	 * @param signNo
	 * @param peroid
	 * @param inInvestRiskDataSet
	 * @return
	 */
	private double[][] getPolSubDatas1(String type, String signNo, int peroid,
			PolSubDataBean[] inInvestRiskDataSet, int appage) {
		List<PolSubDataBean> tempInvestRiskDataSet = new ArrayList();
		int size = 0;
		if (signNo != null && !signNo.equals("")
				&& (size = inInvestRiskDataSet.length) > 0) {
			for (int i = 0; i < size; i++) {
				PolSubDataBean tempInvestRiskDataSchema = inInvestRiskDataSet[i];
				if (type.equals(tempInvestRiskDataSchema.type)
						&& tempInvestRiskDataSchema.signNo.equals(signNo)) {
					tempInvestRiskDataSet.add(tempInvestRiskDataSchema);
				}
			}
		}

		double result[][] = new double[peroid + 1][5];
		Arrays.fill(result, 0);
		int counts = 0;
		if ((counts = tempInvestRiskDataSet.size()) > 0) {
			int index = 1;
			result[0] = new double[] { 0, 0, 0, 0, 0 };
			for (; index < peroid + 1; index++) {
				// int absIndex = index + 1;
				double valued = 0;
				double value0 = 0;
				double value1 = 0;
				double value2 = 0;
				double value3 = 0;
				double value4 = 0;
				double value5 = 0;
				double[] rss = new double[7];
				for (int j = 0; j < counts; j++) {
					PolSubDataBean tempInvestRiskDataSchema = tempInvestRiskDataSet.get(j);
					String sage = tempInvestRiskDataSchema.age;
					
					String sdegree = tempInvestRiskDataSchema.degree;
					String svalue=tempInvestRiskDataSchema.value;
					String srs1 = tempInvestRiskDataSchema.protagonist;
					String srs2 = tempInvestRiskDataSchema.exponent;
					String srs3 = tempInvestRiskDataSchema.banlance;
					String srs4 = tempInvestRiskDataSchema.solidity;
					String srs5 = tempInvestRiskDataSchema.prevent;
					
					sage = sage ==null? "0":sage;
					
					sdegree = sdegree ==null? "0":sdegree;
					svalue= svalue ==null? "0":svalue;
					srs1 = srs1 ==null? "0":srs1;
					srs2 = srs2 ==null? "0":srs2;
					srs3 = srs3 ==null? "0":srs3;
					srs4 = srs4 ==null? "0":srs4;
					srs5 = srs5 ==null? "0":srs5;
					
					int age = Integer.valueOf(sage);
					
					int degree = Integer.valueOf(sdegree);
                    double value=Double.valueOf(svalue);
					double rs1 = Double.valueOf(srs1);
					double rs2 = Double.valueOf(srs2);
					double rs3 = Double.valueOf(srs3);
					double rs4 = Double.valueOf(srs4);
					double rs5 = Double.valueOf(srs5);
					
					if (age == index + appage) {
						// ??��???
						valued+=degree;
						value0+=value;
						value1 += rs1;
						value2 += rs2;
						value3 += rs3;
						value4 += rs4;
						value5 += rs5;
					}
				}
				rss[0] = valued;
				rss[1] = value0;
				rss[2] = value1;
				rss[3] = value2;
				rss[4] = value3;
				rss[5] = value4;
				rss[6] = value5;
				// ??????,??????
				if (index > 1
						&& rss[0] + rss[1] + rss[2] + rss[3] + rss[4]+rss[5]+rss[6] == 0) {
					rss = new double[] { result[index - 1][0],
							result[index - 1][1], result[index - 1][2],
							result[index - 1][3], result[index - 1][4],
							result[index - 1][5], result[index - 1][6]};
				}
				result[index] = rss;
			}
		}
		return result;
	}

	// ///////////////////////////////////////////////////////////////////////////////////////////////
	// ////??????? ???????? ?? ???
	// //////////////////////////////////////////////////////////////////
	// //////////////////////////////////////////////////////////////////////////////////////////////
	/**
	 * ???????????[??????? ???????? ??]
	 * 
	 * @param type
	 * @param signNo
	 * @param peroid
	 * @param inInvestRiskDataSet
	 * @return
	 */
	private double[] getPolSubData2(String type, String signNo, int peroid,
			PolSubDataBean[] inInvestRiskDataSet, int appage) {
		List<PolSubDataBean> tempInvestRiskDataSet = new ArrayList();
		int size = 0;
		if (signNo != null && !signNo.equals("")
				&& (size = inInvestRiskDataSet.length) > 0) {
			for (int i = 0; i < size; i++) {
				PolSubDataBean tempInvestRiskDataSchema = inInvestRiskDataSet[i];
				if (type.equals(tempInvestRiskDataSchema.type)
						&& tempInvestRiskDataSchema.signNo.equals(signNo)) {
					tempInvestRiskDataSet.add(tempInvestRiskDataSchema);
				}
			}
		}

		double result[] = new double[peroid + 1];
		Arrays.fill(result, 0);
		int counts = 0;
		if ((counts = tempInvestRiskDataSet.size()) > 0) {
			int index = 1;
			result[0] = 0;
			for (; index < peroid + 1; index++) {
				// int absIndex = index + 1;
				double value = 0;
				for (int j = 0; j < counts; j++) {
					PolSubDataBean tempInvestRiskDataSchema = tempInvestRiskDataSet.get(j);
					
					String sage = tempInvestRiskDataSchema.age;
					String sdegree = tempInvestRiskDataSchema.degree;
					String srs = tempInvestRiskDataSchema.value;
					
					sage = sage==null?"0":sage;
					sdegree =sdegree==null?"0":sdegree;
					srs = srs==null?"0":srs;
					
					int age = Integer.valueOf(sage);
					int degree = Integer.valueOf(sdegree);
					double rs = Double.valueOf(srs);
					if (age <= index + appage && degree >= index + appage) {
						// ??��???
						value += rs;
					}
				}
				result[index] = value;
			}
		}
		return result;
	}

	/**
	 * ???????????[ ??????? ??????????]
	 * 
	 * @param type
	 * @param signNo
	 * @param peroid
	 * @param inInvestRiskDataSet
	 * @return
	 */
	private double[][] getPolSubDatas2(String type, String signNo, int peroid,
			PolSubDataBean[] inInvestRiskDataSet, int appage) {

		return getPolSubDatas1(type, signNo, peroid, inInvestRiskDataSet,
				appage);
	}

	// ///////////////////////////////////////////////////////////////////////////////////////////////
	// ////?????? ???? ?? ???
	// //////////////////////////////////////////////////////////////////
	// //////////////////////////////////////////////////////////////////////////////////////////////
	/**
	 * ???????????[???????????]
	 * 
	 * @param type
	 * @param signNo
	 * @param peroid
	 * @param inInvestRiskDataSet
	 * @return
	 */
	private double[] getPolSubData3(String type, String signNo, int peroid,
			PolSubDataBean[] inInvestRiskDataSet, int appage) {
		List<PolSubDataBean> tempInvestRiskDataSet = new ArrayList();
		int size = 0;
		if (signNo != null && !signNo.equals("")
				&& (size = inInvestRiskDataSet.length) > 0) {
			for (int i = 0; i < size; i++) {
				PolSubDataBean tempInvestRiskDataSchema = inInvestRiskDataSet[i];
				if (type.equals(tempInvestRiskDataSchema.type)
						&& tempInvestRiskDataSchema.signNo.equals(signNo)) {
					tempInvestRiskDataSet.add(tempInvestRiskDataSchema);
				}
			}
		}

		double result[] = new double[peroid + 1];
		Arrays.fill(result, 0);
		int counts = 0;
		if ((counts = tempInvestRiskDataSet.size()) > 0) {
			int index = 1;
			result[0] = 0;
			for (; index < peroid + 1; index++) {
				// int absIndex = index + 1;
				double value = 0;
				for (int j = 0; j < counts; j++) {
					PolSubDataBean tempInvestRiskDataSchema = tempInvestRiskDataSet.get(j);
					String sage = tempInvestRiskDataSchema.age;
					String sdegree = tempInvestRiskDataSchema.degree;
					String srs = tempInvestRiskDataSchema.value;
					
					sage = sage == null? "0":sage;
					sdegree = sdegree == null? "0":sdegree;
					srs = srs == null? "0":srs;
					
					int age = Integer.valueOf(sage);
					int degree = Integer.valueOf(sdegree);
					double rs = Double.valueOf(srs);
					if (age <= index && age + degree > index) {
						// ??��???
						value += rs;
					}
				}
				result[index] = value;
			}
		}
		return result;
	}

	/**
	 * ???????????[ ?????? ??????????]
	 * 
	 * @param type
	 * @param signNo
	 * @param peroid
	 * @param inInvestRiskDataSet
	 * @return
	 */
	private double[][] getPolSubDatas3(String type, String signNo, int peroid,
			PolSubDataBean[] inInvestRiskDataSet, int appage) {
		List<PolSubDataBean> tempInvestRiskDataSet = new ArrayList();
		int size = 0;
		if (signNo != null && !signNo.equals("")
				&& (size = inInvestRiskDataSet.length) > 0) {
			for (int i = 0; i < size; i++) {
				PolSubDataBean tempInvestRiskDataSchema = inInvestRiskDataSet[i];
				if (type.equals(tempInvestRiskDataSchema.type)
						&& tempInvestRiskDataSchema.signNo.equals(signNo)) {
					tempInvestRiskDataSet.add(tempInvestRiskDataSchema);
				}
			}
		}

		double result[][] = new double[peroid + 1][5];
		Arrays.fill(result, 0);
		int counts = 0;
		if ((counts = tempInvestRiskDataSet.size()) > 0) {
			int index = 1;
			result[0] = new double[] { 0, 0, 0, 0, 0 };
			for (; index < peroid + 1; index++) {
				// int absIndex = index + 1;
				double valued = 0;
				double value0 = 0;
				double value1 = 0;
				double value2 = 0;
				double value3 = 0;
				double value4 = 0;
				double value5 = 0;
				double[] rss = new double[5];
				for (int j = 0; j < counts; j++) {
					PolSubDataBean tempInvestRiskDataSchema = tempInvestRiskDataSet.get(j);
					
					String sage = tempInvestRiskDataSchema.age;
					
					String sdegree = tempInvestRiskDataSchema.degree;
					String svalue=tempInvestRiskDataSchema.value;
					String srs1 = tempInvestRiskDataSchema.protagonist;
					String srs2 = tempInvestRiskDataSchema.exponent;
					String srs3 = tempInvestRiskDataSchema.banlance;
					String srs4 = tempInvestRiskDataSchema.solidity;
					String srs5 = tempInvestRiskDataSchema.prevent;
					
					sage = sage==null?"0":sage;
					svalue = svalue==null?"0":svalue;
					srs1 = srs1==null?"0":srs1;
					srs2 = srs2==null?"0":srs2;
					srs3 = srs3==null?"0":srs3;
					srs4 = srs4==null?"0":srs4;
					srs5 = srs5==null?"0":srs5;
					
					int age = Integer.valueOf(sage);
					
					int degree = Integer.valueOf(svalue);
					double value=Double.valueOf(svalue);
					double rs1 = Double.valueOf(srs1);
					double rs2 = Double.valueOf(srs2);
					double rs3 = Double.valueOf(srs3);
					double rs4 = Double.valueOf(srs4);
					double rs5 = Double.valueOf(srs5);
					if (age == index) {
						// ??��???
						valued += degree;
						value0 += value;
						value1 += rs1;
						value2 += rs2;
						value3 += rs3;
						value4 += rs4;
						value5 += rs5;
					}
				}
				rss[0] = valued;
				rss[1] = value0;
				rss[2] = value1;
				rss[3] = value2;
				rss[4] = value3;
				rss[5] = value4;
				rss[6] = value5;

				// ??????,??????
				if (index > 1
						&& rss[0] + rss[1] + rss[2] + rss[3] + rss[4]+rss[5]+rss[6] == 0) {
					rss = new double[] { result[index - 1][0],
							result[index - 1][1], result[index - 1][2],
							result[index - 1][3], result[index - 1][4],
							result[index - 1][5], result[index - 1][6]};
				}

				result[index] = rss;

			}
		}
		return result;
	}

	// ///////////////////////////////////////////////////////////////////////////////////////////////
	// ////?????? ??????? ?? ???
	// //////////////////////////////////////////////////////////////////
	// //////////////////////////////////////////////////////////////////////////////////////////////
	/**
	 * ???????????[???????????]
	 * 
	 * @param type
	 * @param signNo
	 * @param peroid
	 * @param inInvestRiskDataSet
	 * @return
	 */
	private double[] getPolSubData4(String type, String signNo, int peroid,
			PolSubDataBean[] inInvestRiskDataSet, int appage) {
		List<PolSubDataBean> tempInvestRiskDataSet = new ArrayList();
		int size = 0;
		if (signNo != null && !signNo.equals("")
				&& (size = inInvestRiskDataSet.length) > 0) {
			for (int i = 0; i < size; i++) {
				PolSubDataBean tempInvestRiskDataSchema = inInvestRiskDataSet[i];
				if (type.equals(tempInvestRiskDataSchema.type)
						&& tempInvestRiskDataSchema.signNo.equals(signNo)) {
					tempInvestRiskDataSet.add(tempInvestRiskDataSchema);
				}
			}
		}

		double result[] = new double[peroid + 1];
		Arrays.fill(result, 0);
		int counts = 0;
		if ((counts = tempInvestRiskDataSet.size()) > 0) {
			int index = 1;
			result[0] = 0;
			for (; index < peroid + 1; index++) {
				// int absIndex = index + 1;
				double value = 0;
				for (int j = 0; j < counts; j++) {
					PolSubDataBean tempInvestRiskDataSchema = tempInvestRiskDataSet.get(j);
					
					String sage = tempInvestRiskDataSchema.age;
					String sdegree = tempInvestRiskDataSchema.degree;
					String srs = tempInvestRiskDataSchema.value;
					
					sage = sage==null?"0":sage;
					sdegree= sdegree==null?"0":sdegree;
					srs= srs==null?"0":srs;
					
					int age = Integer.valueOf(sage);
					int degree = Integer.valueOf(sdegree);
					double rs = Double.valueOf(srs);
					if (age <= index && degree >= index) {
						// ??��???
						value += rs;
					}
				}
				result[index] = value;
			}
		}
		return result;
	}

	/**
	 * ???????????[ ?????? ??????????]
	 * 
	 * @param type
	 * @param signNo
	 * @param peroid
	 * @param inInvestRiskDataSet
	 * @return
	 */
	private double[][] getPolSubDatas4(String type, String signNo, int peroid,
			PolSubDataBean[] inInvestRiskDataSet, int appage) {
		return getPolSubDatas3(type, signNo, peroid, inInvestRiskDataSet,
				appage);
	}

}
